package com.bawei.mdm2207alast.model

import android.app.DownloadManager
import android.app.DownloadManager.Request
import android.content.Context
import android.net.Uri
import android.os.Environment

/**
 * 作者：赵亮
 * 时间：2024/12/17 11:59
 * 功能：下载工具类
 */
object DownloadUtil {

    /**
     * 下载工具类 封装
     */
    fun download(context: Context, url: String, fileName: String) {
        // 1. 创建下载请求任务
        val request = Request(Uri.parse(url))

        // 2. 在通知栏显示
        request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        request.setTitle(fileName)
        request.setDescription("下载$fileName")
        //设置类型为.apk
        request.setMimeType("application/vnd.android.package-archive")

        // 3. 设置保存路径
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)


        // 4. 获取下载任务管理器
        val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

        // 5. 执行任务
        val downloadId = downloadManager.enqueue(request)
    }
}